<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class product_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		
	public function create_user_type($insert)
	{
		$this->db->insert('user_type',$insert);
	}
	
	public function create_department($insert)
	{
		$this->db->insert('department',$insert);
	}
	
	public function add_group($name,$parent,$additional_data){
		
		$data = array(
			    'name'   => $name,
				'parent'   => $parent
		 );
		// print_r($data);exit;
		$this->db->insert('acc_group',$data);
			
	}
	
	public function get_category(){
			$q = $this->db->get('category');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}
	
	public function get_unit(){
			$q = $this->db->get('unit');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}
	
	public function get_company(){
		$q = $this->db->get('company');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
			return $data;
		}
		
	}
	
	
		
	public function get_group(){
	$q = $this->db->get('acc_group');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}
	
	public function get_branchlist(){
	$q = $this->db->get('branch');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}



	public function get_productList(){

	$this->db->select('product.*, category.name AS category_name, company.name AS company_name');
	$this->db->from('product', 'company', 'category');
	$this->db->join('category', 'product.id_category= category.id');
		$this->db->join('company', 'product.id_company= company.id');
	//$this->db->join('company', 'product.id_company= company.id');
	$result = $this->db->get();
	return $result;



		//echo "i am here";exit;
/*		
		$this->db->select('p.*');
		$this->db->from('product p, category cg, company cm');
	//	$this->db->where('product.group ='.$id);
		$this->db->join('category cg','p.id_category=cg.id');
		$this->db->join('company cm','p.id_company=cm.id');
		 $query = $this->db->get();
		echo "i am here";
		print_r($query->result());exit;*/
	
	
		//$query = $this->db->get('product');
	//eturn $query;

		
		
		} 

public function get_product($id){
	$query = $this->db->get_where('product', array('id' => $id));
	return $query;
	}

public function insert($data){
		$this->db->insert('product',$data);
		$data1['id_product'] = $this->db->insert_id();;
		$data1['opening_stock'] = $data['opening_stock'];
		if($data['id_branch']!='0'){
		$data1['id_branch']= $data['id_branch'];	
		$this->db->insert('branch_product',$data1);
		}else{
		$this->db->insert('warehouse',$data1);
		}
	
	}

/*public function get_groups(){
	$this->db->select('acc_group.*,projects.name');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$id);
		$this->db->join('projects','tasks.project_id=projects.id');
		return $this->db->get()->row_array();
		$query = $this->db->get('acc_group');
	return $query;
}
*/
public function edit_product($id){
		$query = $this->db->get_where('product', array('id' => $id));
		return $query;
	}

public function update($edit_id, $data, $branch_product){
	
		$this->db->update('product',$data,array('id'=>$edit_id));
		if($data['id_branch']!='0'){
			//print_r($branch_product);exit;
		$this->db->update('branch_product',$branch_product,array('id'=>$branch_product['id']));
			
		}else{
		$this->db->update('warehouse',$branch_product,array('id'=>$branch_product['id']));		
			
		}
	
	}

public function delete($id){
		$this->db->delete('product',array('id'=>$id));		
	}


public function getproductByID($id){
		$query = $this->db->get_where('product', array('id' => $id));
		return $query->row_array();
	
	}


public function check_product($name,$group){
		$query = $this->db->get_where('product', array('name' => $name,'group' => $group));
		$n=$query->num_rows();
		if($n >0){
			
			return 1;
			}else{
			return 0;
				}
	
	}


public function get_branch_product(){
	
	$this->db->select('branch_product.*, branch.name AS branch_name');
	$this->db->from('branch_product', 'branch');
	$this->db->join('branch', 'branch_product.id_branch = branch.id');
	//	$this->db->join('company', 'product.id_company= company.id');
	//$this->db->join('company', 'product.id_company= company.id');
	$result = $this->db->get();
	 return $result;
	
}

public function get_warehouse_product(){
	
	$this->db->select('product.id as id ,product.code, category.name AS category_name, company.name AS company_name,product.purchase_price,product.selling_price,product.opening_stock,product.alert_quantity');
	$this->db->from('warehouse','product', 'company', 'category');
	$this->db->join('product', 'warehouse.id_product= product.id');
	$this->db->join('category', 'product.id_product= category.id');
	$this->db->join('company', 'product.id_company= company.id');
	$result = $this->db->get()->result_array();
	print_r($result);exit;
	
}
public function  get_product_details($code){
	//$query = $this->db->get_where('product', array('code' => $code));
	//return $query;	
	
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name' );
	$this->db->from('product', 'category','company' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->where(array('product.code' => $code));
	$q = 	$query = $this->db->get();
	$product = $q->row_array();
	$product_name = $code.",".$product['category_name'].",".$product['company_name'];
	return  $product_name; 
}


public function get(){
	   $this->datatables
			->select('product.id as id ,product.name,product.code, category.name AS category_name, company.name AS company_name,product.purchase_price,product.selling_price,product.opening_stock,unit.name AS unit_name,product.alert_quantity')
			->from('product', 'company', 'category')
			->join('category', 'product.id_category= category.id')
			->join('company', 'product.id_company= company.id')
			->join('unit', 'product.id_unit= unit.id')
			->add_column("Actions", 
			"<center>			<a href='edit_product/$1' title='Edit Product'>
								<i class='icon-edit'></i>
								</a>&nbsp;&nbsp;&nbsp;
							    <a href='delete_product/$1' title='Delete Product' onClick=\"return confirm('". 'Do U Want To Delete Category! ' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}


	public function get_branch(){
	  $this->datatables
			->select('branch_product.id as id,branch.name as branch_name, product.code,product.name as product_name,category.name as category_name, company.name as company_name,branch_product.opening_stock as branch_stock,')
			->from('branch_product')
			->join('product', 'branch_product.id_product= product.id')
			->join('branch', 'branch_product.id_branch= branch.id')
			->join('category', 'product.id_category= category.id')
			->join('company', 'product.id_company= company.id')
			/*->add_column("Actions", 
			"<center>			<a href='edit_product/$1'>
								<i class='icon-edit'></i>
								</a>
							    <a href='delete_product/$1' onClick=\"return confirm('". 'Do U Want To Delete Category! ' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")*/
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}


public function get_warehouse(){
	   $this->datatables
			->select('warehouse.id as id, product.code,product.name as product_name,category.name as category_name, company.name as company_name,warehouse.opening_stock as warehouse_stock,')
			->from('warehouse')
			->join('product', 'warehouse.id_product= product.id')
			->join('category', 'product.id_category= category.id')
			->join('company', 'product.id_company= company.id')
			/*->add_column("Actions", 
			"<center>			<a href='edit_product/$1'>
								<i class='icon-edit'></i>
								</a>
							    <a href='delete_product/$1' onClick=\"return confirm('". 'Do U Want To Delete Category! ' ."')\">
								<i class='icon-trash'></i>
								</a></center>", "id")*/
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}

public function check_category($name){
		$query = $this->db->get_where('category', array('name' => $name));
		return $query->num_rows();

}
public function check_company($name){
		$query = $this->db->get_where('company', array('name' => $name));
		return $query->num_rows();
	
}
public function check_unit($name){
		$query = $this->db->get_where('unit', array('name' => $name));
		return $query->num_rows();
	
}


public function insert_category($data){
		$this->db->insert('category',$data);
		return $this->db->insert_id();;
		
	}
	
public function insert_company($data){
		$this->db->insert('company',$data);
		return $this->db->insert_id();;
	}

public function insert_unit($data){
		$this->db->insert('unit',$data);
		return $this->db->insert_id();;
		
	}


public function edit_branchproduct($id){
		$query = $this->db->get_where('branch_product', array('id_product' => $id));
		return $query;
	
}
public function edit_warehouseproduct($id){
		$query = $this->db->get_where('warehouse', array('id_product' => $id));
		return $query;
	
}

public function  find_product($id_branch, $id_product){
if($id_branch == '0'){
		$query = $this->db->get_where('warehouse', array('id_product' => $id_product));
		$data =  $query->row_array();
		if(count($data)==0){
			return  0; 
		}else{
			return $data;
		}		
 }else{
		$query = $this->db->get_where('branch_product', array('id_branch' => $id_branch,'id_product' => $id_product));
		$data =  $query->row_array();
 		if(count($data)== 0){
			return 0;
		}else{
			return $data;
			
		}
 
 }
	
}

public function update_branch($id_branch, $id_product, $opening_stock,$branch_rack){
		$product_query = $this->db->get_where('product', array('id' => $id_product));
			$product_data = $product_query->row_array(); 
			$actual_stock =$product_data['opening_stock'];
	if($id_branch == '0'){
			$query = $this->db->get_where('warehouse', array('id_product' => $id_product));
			$data =  $query->row_array();
			if(count($data)==0){
				$temp_stock = intval($actual_stock)+intval($opening_stock);
				$this->db->update('product',array('opening_stock'=>$temp_stock),array('id'=>$id_product));
				$insert_data = array('id_product'=>$id_product,'opening_stock'=>$opening_stock,'branch_rack'=>$branch_rack);
				if($this->db->insert('warehouse',$insert_data)){
					echo "Stock Inserted Into Warehouse!";
				}
			}else{
				$temp_stock = intval($actual_stock)- intval($data['opening_stock'])+intval($opening_stock);
				$this->db->update('product',array('opening_stock'=>$temp_stock),array('id'=>$id_product));
				$update_data = array('opening_stock'=>$opening_stock,'branch_rack'=>$branch_rack);
				if($this->db->update('warehouse',$update_data, array('id'=>$data['id']))){
				echo "Stock updated Into Warehouse!";
				}
			}		
		 }else{
			$query = $this->db->get_where('branch_product', array('id_branch' => $id_branch,'id_product' => $id_product));
			$data =  $query->row_array();
 			if(count($data)== 0){
				$temp_stock = intval($actual_stock)+intval($opening_stock);
				$this->db->update('product',array('opening_stock'=>$temp_stock),array('id'=>$id_product));
				$insert_data = array('id_branch'=>$id_branch,'id_product'=>$id_product,'opening_stock'=>$opening_stock,'branch_rack'=>$branch_rack);
					if($this->db->insert('branch_product',$insert_data)){
					echo "Stock Inserted Into Branch!";		
				}
			}else{
				$temp_stock = intval($actual_stock)- intval($data['opening_stock'])+intval($opening_stock);
				$this->db->update('product',array('opening_stock'=>$temp_stock),array('id'=>$id_product));
				$update_data = array('opening_stock'=>$opening_stock,'branch_rack'=>$branch_rack);
				if($this->db->update('branch_product',$update_data, array('id'=>$data['id']))){
						echo "Stock Updated Into Branch!";
				}
			
			}
 
	 }
	
}
public function get_category_product($category)
{

$data=$this->db->get_where('category', array('name' => $category))->num_rows();	
if($data !==0)
{
$category_id=$this->db->get_where('category', array('name' => $category))->row();
return $category_id->id;
}
else
{
$data1=array('name'=>$category,);
$this->db->insert('category',$data1);
return $this->db->insert_id();
}
}	
public function get_company_product($company)
{
$data=$this->db->get_where('company', array('name' => $company))->num_rows();	
if($data !==0)
{
$company_id=$this->db->get_where('company', array('name' => $company))->row();
return $company_id->id;
}
else
{
$data1=array('name'=>$company,);
$this->db->insert('company',$data1);
return $this->db->insert_id();
}
}

public function get_unit_product($unit)
{
$data=$this->db->get_where('unit', array('name' => $unit))->num_rows();	
if($data !==0)
{
$unit_id=$this->db->get_where('unit', array('name' => $unit))->row();
return $unit_id->id;
}
else
{
$data1=array('name'=>$unit,);
$this->db->insert('unit',$data1);
return $this->db->insert_id();
}
}

public function insert_product($data,$code)
{
$product_num=$this->db->get_where('product', array('code' =>$code))->num_rows();	
if($product_num !==0)
{
$this->db->update('product',$data,array('code' => $code));
}
else
{
$this->db->insert('product',$data);


}


}



}
